<?php

namespace App\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;

class UserListened extends Model
{
    protected $table = 'user_listened';
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function item()
    {
        return $this->morphTo();
    }

    public static function addListened($item, $user)
    {
        $ul = $item->listened()->where('user_id', $user->id)->firstOrCreate([], [
            'user_id' => $user->id,
            'parent_id' => $item->doc->id ?? 0,
            'liveroom_id' => $item->user->liveroom->id ?? 0,
            'l_user_id' => $item->user->id ?? 0,
            'qd_id' => $user->qd_id ?? 0
        ]);
        $ul->increment('num');
    }
}
